package cn.zjitc.service.impl;

import cn.zjitc.dao.AdminBookDao;
import cn.zjitc.entity.PageResult;
import cn.zjitc.entity.QueryPageBean;
import cn.zjitc.pojo.Book;
import cn.zjitc.service.AdminBookService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = AdminBookService.class)
@Transactional
public class AdminBookServiceImpl implements AdminBookService {
    @Autowired
    private AdminBookDao adminBookDao;

    @Override
    public List<Book> findAll() {
        return adminBookDao.findAll();
    }

    @Override
    public Book findById(Integer id) {
        return adminBookDao.findById(id);
    }


    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);
        Page<Book> page = adminBookDao.findByCondition(queryString);
        long total = page.getTotal();
        List<Book> rows = page.getResult();
        return new PageResult(total,rows);
    }

    @Override
    public void add(Book book) {
        adminBookDao.add(book);
    }

    @Override
    public void deleteById(Integer id) {
        adminBookDao.deleteById(id);
    }

    @Override
    public void edit(Book search) {
        adminBookDao.edit(search);
    }
}
